package org.lanqiao.api.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.lanqiao.common.Constants;
import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.pojo.UserPOJO;
import org.lanqiao.service.UserServiceImpl;

/**
 * Servlet implementation class AlterUserServlet
 */
@WebServlet(description = "这是一个修改用户信息的接口", urlPatterns = { "/api/user/alter" })
public class AlterUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AlterUserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    UserPOJO user = new UserPOJO();
	    user.setUserId(request.getParameter("userId"));
        user.setUserName(request.getParameter("userName"));
        user.setPassword(request.getParameter("password"));
        user.setAddres(request.getParameter("address"));
        user.setAge(request.getParameter("age"));
        user.setBirthday(request.getParameter("birthday"));
        user.setComment(request.getParameter("comment"));
        user.setPhoto(request.getParameter("photo"));
        user.setSex(request.getParameter("sex"));
        user.setTelphone(request.getParameter("telphone"));
        user.setStatus(request.getParameter("status"));
        
        //创建一个JsonResult封装消息，然后转为json，变成一个对象{}
        JsonResult result=null;
        //获得当前请求的Session对象，如果上下文中没有绑定的Session，创建一个session，如果有就返回session
        HttpSession session=request.getSession();
        UserServiceImpl usi=new UserServiceImpl();
        List <HashMap<String,Object>>list=null;
        try {
            list = usi.alter(user);
        }catch(Exception ex){
            result=new JsonResult ("用户修改异常",Constants.STATUS_FAILURE,ex.getMessage());
        }

        if(list!=null&&list.size()>0) {
            result=new JsonResult ("用户修改成功",Constants.STATUS_SUCCESS, list);
        }else {
            result=new JsonResult<LinkedHashMap<String,Object>>("信息违反规定",Constants.STATUS_UNFOUND);
        }
        
        //将响应和结果  数据，转为json
        JsonResultWriter.writer(response, result);
	}

}
